package co.fitstart.entity.user.distributor;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.demo2do.core.format.PersistentFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.order.Order;
import co.fitstart.entity.user.User;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "distributor_user_relation")
@PersistentFormat
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class DistributorUserRelation {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 发生时间 */
    private Date createTime;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Distributor distributor;
    
    /** 分销用户openid */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private User user;
    
    @Column(name = "is_consumed")
    private boolean consumed;
    
    /** 消费订单 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Order order;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public DistributorUserRelation() {
        
    }
    
    /**
     * 
     * @param distributor
     * @param openid
     */
    public DistributorUserRelation(Distributor distributor, User user) {
        this.distributor = distributor;
        this.createTime = new Date();
        this.user = user;
        this.disabled = false;
        this.consumed = false;
    }
    
    /**
     * 
     * @param order
     * @return
     */
    public DistributorUserRelation consume(Order order) {
        this.order = order;
        this.consumed = true;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public DistributorUserRelation disable() {
        this.disabled = true;
        return this;
    }
    
    public int getEarnedPrice() {
        int orderPrice = this.order.getPrice();
        return new BigDecimal(orderPrice)
                  .divide(new BigDecimal(100))
                  .multiply(new BigDecimal(this.distributor.getKickback()))
                  .intValue();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the distributor
     */
    public Distributor getDistributor() {
        return distributor;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @return the consumed
     */
    public boolean isConsumed() {
        return consumed;
    }

    /**
     * @return the order
     */
    public Order getOrder() {
        return order;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param distributor the distributor to set
     */
    public void setDistributor(Distributor distributor) {
        this.distributor = distributor;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @param consumed the consumed to set
     */
    public void setConsumed(boolean consumed) {
        this.consumed = consumed;
    }

    /**
     * @param order the order to set
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
